# -*- coding: utf-8 -*-#
# -------------------------------------------------------------------------------
# Name:         parse_bag
# Date:         2022/8/20
# Description:  将配置好topic的点云与图片保存成本地文件
# -------------------------------------------------------------------------------
import os 
import copy
import struct
from pathlib import Path
from traceback import format_exc

import cv2
import numpy as np
import rosbag
import sensor_msgs.point_cloud2 as pc2
from cv_bridge import CvBridge

PCD_ASCII_TEMPLATE = """# .PCD v0.7 - Point Cloud Data file format
VERSION 0.7
FIELDS x y z i
SIZE 4 4 4 2
TYPE F F F U
COUNT 1 1 1 1
WIDTH {}
HEIGHT 1
VIEWPOINT 0 0 0 1 0 0 0
POINTS {}
DATA ascii
"""

PCD_BINARY_TEMPLATE = """# .PCD v0.7 - Point Cloud Data file format
VERSION 0.7
FIELDS x y z i
SIZE 4 4 4 4
TYPE F F F F
COUNT 1 1 1 1
WIDTH {}
HEIGHT 1
VIEWPOINT 0 0 0 1 0 0 0
POINTS {}
DATA binary
"""


class BagExtractor:
    def __init__(self, bag_folder, dst_folder):
        self.bag_folder = Path(bag_folder)
        self.dst_folder = Path(dst_folder)
        self.bridge = CvBridge()

    def extract_pcd_img(self, pcd_topic_list: list, img_topic_list: list):
        """
        :param pcd_topic_list: 点云文件topic名字列表
        :param img_topic_list: 图片文件topic名字列表
        :return:
        """
        for bag_file in self.bag_folder.rglob("*.bag"):
            output_file = self.dst_folder.joinpath(bag_file.relative_to(self.bag_folder))
            output_folder = output_file.parent.joinpath(f"{output_file.stem}")
            output_folder.mkdir(parents=True, exist_ok=True)
            with rosbag.Bag(bag_file, 'r') as bag:
                info = bag.get_type_and_topic_info()
                print(info.topics)
                print('*************************************************')
                print(info.msg_types)
                pcd_number = self.get_pcd_img_number(info, pcd_topic_list)
                img_number = self.get_pcd_img_number(info, img_topic_list)
                print(f"解析{bag_file.name}  pcd总数:{pcd_number}  图片总数:{img_number}")
                num = 0
                for topic, msg, t in bag.read_messages():
                    num += 1
                    #time_str = "%.9f" % msg.header.stamp.to_sec()
                    if topic in pcd_topic_list:  # 点云的topic
                        # time_str = "%.9f" % msg.header.stamp.to_sec()
                        pcd_file_name = topic.split("/")[-2] + "_" + f"{num}.pcd"     #解析后保存的pcd文件名
                        pcd_path = output_folder.joinpath(pcd_file_name)
                        # self.to_pcd_ascii(pcd_path, msg)
                        self.to_pcd_binary(pcd_path, msg)
                        print("Extract pcd file %s."%pcd_file_name)
                    if topic in img_topic_list:  # 图片的topic
                       #if topic.find("sensor_msgs/Image") > -1:
                        # time_str = "%.9f" % msg.header.stamp.to_sec()
                        img_file_name = topic.split("/")[-2] + "_" + f"{num}.jpg"     #解析后保存的jpg文件名
                        img_path = output_folder.joinpath(img_file_name)
                        self.to_img(img_path, msg)
                        print("Extract img file %s."%img_file_name)

    @classmethod
    def get_pcd_img_number(cls, info, topic_list):
        try:
            count = 0
            for i in range(len(topic_list)):
                for topic in topic_list[i].split():
                    topic_ob = info.topics.get(topic, None)
                    if topic_ob:
                        count = topic_ob.message_count + count
            return count
        except Exception as e:
            print(f"获取pcd|img帧数出错:{e}")

    @classmethod
    def to_pcd_ascii(cls, pcd_path, msg):
        with open(pcd_path, 'w') as f:
            points_data = np.array(list(pc2.read_points(msg)))
            lidar = list(np.delete(points_data, np.where(np.isnan(points_data))[0], axis=0))
            header = copy.deepcopy(PCD_ASCII_TEMPLATE).format(len(lidar), len(lidar))
            f.write(header)
            for pi in lidar:
                f.write(' '.join([str(i) for i in pi]) + '\n')

    @classmethod
    def to_pcd_binary(cls, pcd_path, msg):
        with open(pcd_path, 'wb') as f:
            points_data = np.array(list(pc2.read_points(msg)))
            lidar = list(np.delete(points_data, np.where(np.isnan(points_data))[0], axis=0))
            header = copy.deepcopy(PCD_BINARY_TEMPLATE).format(len(lidar), len(lidar))
            f.write(header.encode())
            for pi in lidar:
                h = struct.pack('ffff', pi[0], pi[1], pi[2], pi[3])
                f.write(h)

    def to_img(self, img_path, msg):
        try:
            # cv_image = self.bridge.compressed_imgmsg_to_cv2(msg)
            cv_image = self.bridge.imgmsg_to_cv2(msg)
            cv2.imencode('.jpg', cv_image)[1].tofile(str(img_path))
        except Exception as e:
            print(f"生成图片失败:{e}")

    def def_files(self, dst_path):
        try:
            for root, dirs, files in os.walk(dst_path):
                for name in files:
                    if name.endswith(".pcd") or name.endswith(".jpg"):
                        os.remove(os.path.join(root, name))
        except Exception as e:
            print(f"删除文件失败:{e}")


if __name__ == '__main__':
    current_path = os.getcwd()                            #当前路径
    bag_path =  current_path + "/rosbag"      # bag文件路径
    dst_path = current_path + "/result"          # 结果路径
    try:
        extractor = BagExtractor(bag_path, dst_path)
        extractor.def_files(dst_path)                  
        extractor.extract_pcd_img(pcd_topic_list=['/kitti/velo/pointcloud'],
                                  img_topic_list=['/kitti/camera_color_left/image_raw', '/kitti/camera_color_right/image_raw', '/kitti/camera_gray_left/image_raw', '/kitti/camera_gray_right/image_raw'])
    except Exception as ee:
        print(f"运行失败,无法解决请联系开发人员!{format_exc()}{ee}")